package com.guaju.yueshangLife.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.databinding.MainBottomItemViewBinding;

/**
 * Created by guaju on 2018/9/10.
 */

public class MainBottomItemView extends FrameLayout {

    private View view;
    private LayoutInflater layoutInflater;
    private MainBottomItemViewBinding binding;
    private String tab;

    public MainBottomItemView(@NonNull Context context) {
        super(context);
        init();
    }

    public MainBottomItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.bottom_item_style);
        String title = a.getString(R.styleable.bottom_item_style_bottomTitle);
        boolean b = a.getBoolean(R.styleable.bottom_item_style_checked, false);
        tab = a.getString(R.styleable.bottom_item_style_tab);
        setChecked(b);
        setBottomItemTitle(title);

    }

    private void init() {
        layoutInflater = LayoutInflater.from(getContext());
        binding = DataBindingUtil.inflate(layoutInflater, R.layout.main_bottom_item_view, this, true);
        View root = binding.getRoot();
        FrameLayout.LayoutParams layoutParams = (LayoutParams) root.getLayoutParams();
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        root.setLayoutParams(layoutParams);


    }


    public void setBottomItemTitle(String title) {
        if (binding != null && !TextUtils.isEmpty(title)) {
            binding.bottomItemTitle.setText(title);
        }
    }


    public void setChecked(boolean b) {
        if (b) {
            binding.bottomItemTitle.setTextColor(getResources().getColor(R.color.checked_text_color));
        } else {
            binding.bottomItemTitle.setTextColor(getResources().getColor(R.color.unchecked_text_color));
        }
        if (TextUtils.equals("home", tab)) {
            binding.bottomItemIcon.setBackgroundResource(b ? R.mipmap.home_checked : R.mipmap.home);
        } else if (TextUtils.equals("cate", tab)) {
            binding.bottomItemIcon.setBackgroundResource(b ? R.mipmap.cate_checked : R.mipmap.category);
        } else if (TextUtils.equals("cart", tab)) {
            binding.bottomItemIcon.setBackgroundResource(b ? R.mipmap.cart_checked : R.mipmap.cart);
        } else if (TextUtils.equals("mine", tab)) {
            binding.bottomItemIcon.setBackgroundResource(b ? R.mipmap.mine_checked : R.mipmap.mine);
        }
    }

    public void reset() {
        binding.bottomItemTitle.setTextColor(getResources().getColor(R.color.unchecked_text_color));
        if (TextUtils.equals("home", tab)) {
            binding.bottomItemIcon.setBackgroundResource(R.mipmap.home);
        } else if (TextUtils.equals("cate", tab)) {
            binding.bottomItemIcon.setBackgroundResource(R.mipmap.category);
        } else if (TextUtils.equals("cart", tab)) {
            binding.bottomItemIcon.setBackgroundResource(R.mipmap.cart);
        } else if (TextUtils.equals("mine", tab)) {
            binding.bottomItemIcon.setBackgroundResource(R.mipmap.mine);
        }
    }
}
